package com.ruoyi.web.controller.publish;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.publish.domain.IdAcceptRegister;
import com.ruoyi.publish.domain.IdIdentsample;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.publish.domain.IdExpert;
import com.ruoyi.publish.service.IIdExpertService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 专家管理Controller
 *
 * @author 张泽望
 * @date 2020-11-03
 */
@Controller
@RequestMapping("/publish/managementt")
public class IdExpertController extends BaseController {
    private String prefix = "publish/managementt";

    @Autowired
    private IIdExpertService idExpertService;

    //    @RequiresPermissions("publish:managementt:view")
    @GetMapping()
    public String managementt() {
        return prefix + "/managementt";
    }

    /**
     * 查询专家管理列表
     */
//    @RequiresPermissions("publish:managementt:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IdExpert idExpert) {
        startPage();
        List<IdExpert> list = idExpertService.selectIdExpertList(idExpert);
        return getDataTable(list);
    }

    //    @RequiresPermissions("publish:managementt:listmoney")
    @PostMapping("/listmoney")
    @ResponseBody
    public TableDataInfo listmoney(IdExpert idExpert) {
        startPage();
        List<IdExpert> list = idExpertService.selectIdExpertListMoney(idExpert);
        return getDataTable(list);
    }

    //    @RequiresPermissions("publish:managementt:viewMoney")
    @GetMapping("/listM")
    public String managementtM() {
        return prefix + "/managementtM";
    }

    /**
     * 专家选择页面
     *
     * @return
     */
    @GetMapping("/expertSelect")
    public String expertSelect() {
        return prefix + "/expertSelect";
    }


    /**
     * 导出专家管理列表
     */
//    @RequiresPermissions("publish:managementt:export")
    @Log(title = "专家管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IdExpert idExpert) {
        List<IdExpert> list = null;
        if (StringUtils.isNotEmpty(idExpert.getExportIds())) {
            list =   idExpertService.selectRecordByIds(idExpert.getExportIds().split(","));
        } else {
            list = idExpertService.selectIdExpertListMoney(idExpert);
        }
        ExcelUtil<IdExpert> util = new ExcelUtil<IdExpert>(IdExpert.class);
        return util.exportExcel(list, "managementt");
    }

    /**
     * 新增专家管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存专家管理
     */
//    @RequiresPermissions("publish:managementt:add")
    @Log(title = "专家管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IdExpert idExpert) {
        idExpert.setCreateBy(ShiroUtils.getLoginName());
        idExpert.setCreateTime(new Date());
        return toAjax(idExpertService.insertIdExpert(idExpert));
    }

    /**
     * 修改专家管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        IdExpert idExpert = idExpertService.selectIdExpertById(id);
        mmap.put("idExpert", idExpert);
        return prefix + "/edit";
    }

    /**
     * 查看专家管理
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        IdExpert idExpert = idExpertService.selectIdExpertById(id);
        mmap.put("idExpert", idExpert);
        return prefix + "/detail";
    }

    /**
     * 修改保存专家管理
     */
//    @RequiresPermissions("publish:managementt:edit")
    @Log(title = "专家管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IdExpert idExpert) {
        return toAjax(idExpertService.updateIdExpert(idExpert));
    }

    /**
     * 删除专家管理
     */
//    @RequiresPermissions("publish:managementt:remove")
    @Log(title = "专家管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(idExpertService.deleteIdExpertByIds(ids));
    }


    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {

        ExcelUtil<IdExpert> util = new ExcelUtil<IdExpert>(IdExpert.class);
        List<IdExpert> list = new ArrayList<>();

        IdExpert idExpert = new IdExpert();
        //专家姓名
        idExpert.setExpertName("张三(示范)");
        //出生年月
        idExpert.setBirthday(new Date());
        //曾任职务
        idExpert.setPositions("专家(示范)");
        //受聘年份
        idExpert.setEmploymentTime(new Date());
        //审读范围
        idExpert.setReviewScope("图书,期刊,发行");
        //递送地址
        idExpert.setDeliveryAddress("重庆(示范)");
        //邮政编码
        idExpert.setPostalCode("400500");
        //电子邮件
        idExpert.setEmail("1248576744@qq.com示范)");
        //联系方式
        idExpert.setBy1("15681231911(示范)");
//        //已审样本数
//        idExpert.setReviewedNumber(Long.valueOf("10"));
//        //稿费
//        idExpert.setManuscriptFee(BigDecimal.valueOf(10));

        list.add(idExpert);
        return util.importDataTemplateExcel(list, "专家数据");
    }

    @Log(title = "专家管理excel导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String year) throws Exception {
        byte[] bytes = file.getBytes();
        InputStream in = new ByteArrayInputStream(bytes);

        ExcelUtil<IdExpert> util = new ExcelUtil<IdExpert>(IdExpert.class);
        List<IdExpert> userList = util.importExcel(in);

        System.out.println("sieze=" + userList.size());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = idExpertService.importIdExpert(userList, updateSupport, operName, year);
        return AjaxResult.success(message);
    }
}
